const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    id_card: '',
    password: '',
    images: [],
    imgs: [],
    has_photo:''
  },

  onLoad(){
   let that = this
    app.getLogin().then(function () {
      that.hasPhoto()
    })
  },

  hasPhoto(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/beforelogin',
      data: {
        access_token: access_token,
      },
      success: (res) => {
        console.log(res)
        if(res.data.code == 1){
          this.setData({
            has_photo:res.data.has_photo
          })
        }
      }
    })
  },

  //选项卡
  chageType: function () {
    let that = this;
    that.setData({
      type: that.data.type == 1 ? 2 : 1,
    });
  },

  getID(e) {
    this.setData({
      id_card: e.detail.value
    })
  },

  getPwd(e) {
    this.setData({
      password: e.detail.value
    })
  },

  resetPwd() {
    wx.navigateTo({
      url: '../resetPwd/resetPwd',
    })
  },

  pwdLogin() {
    if (this.data.id_card == '') {
      wx.showToast({
        title: '请输入身份证号码！',
        icon: 'none'
      })
      return false
    }
    if (this.data.password == '') {
      wx.showToast({
        title: '请输入密码！',
        icon: 'none'
      })
      return false
    }
    let { id_card, password } = this.data
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/idcardlogin',
      data: {
        access_token: access_token,
        id_card: id_card,
        password: password
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          if(this.data.has_photo == 0){
            wx.navigateTo({
              url: '../upload-pic/upload-pic',
            })
          } else {
            wx.reLaunch({
              url: '/pages/check/check',
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: (res) => {
              console.log(res)
            }
          })
        }
      }
    })
  },

  // 图片上传
  // 图片操作的具体函数
  ImageOperator() {
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res)
        // 上传的图片数据
        let imgList = res.tempFilePaths;
        // 原始的图片数据
        let imageList = this.data.images;
        // 原来的图片数量
        let imageLength = imageList.length;
        // 当前的图片数量
        let nowLength = imgList.length;
        if (imageLength < 5) {
          let images = [];
          // 获取缺少的图片张数
          let residue = 5 - imageLength;
          // 如果缺少的张数大于当前的的张数  
          if (residue >= nowLength) {
            // 直接将两个数组合并为一个  
            images = imageList.concat(imgList);
          } else {
            // 否则截取当前的数组一部分  
            images = imageList.concat(imgList.slice(0, residue));
          }
          this.setData({
            images,
            tempImg: res.tempFilePaths[0]
          })
        }
      }
    })
  },
  // 图片获取
  chooseImage() {
    this.ImageOperator()
  },

  // 删除图片
  deleteImage(event) {
    wx.showModal({
      title: '提示',
      content: '确定要删除该照片吗',
      success: (res) => {
        if (res.confirm) {
          let nowIndex = event.currentTarget.dataset.id;
          let images = this.data.images;
          images.splice(nowIndex, 1);
          this.setData({
            images
          })
        }
      }
    })

  },
  // 预览图片
  // previewIamge(event) {
  //   const nowIndex = event.currentTarget.dataset.id;
  //   const images = this.data.images;
  //   wx.previewImage({
  //     current: images[nowIndex],  //当前预览的图片
  //     urls: images,  //所有要预览的图片
  //   })
  // },
  
  // 人脸登录
  faceLogin() {
    if(this.data.images.length == 0){
      wx.showToast({
        title: '请先拍摄或上传照片！',
        icon:'none',
      })
      return false
    }
    wx.showLoading({
      title: '正在校验...',
    })
    let access_token = wx.getStorageSync('access_token')
    // promise上传表单
    let promiseArr = []
    let promise = new Promise((resolve, reject) => {
      wx.uploadFile({
        filePath: this.data.tempImg,
        name: 'file',
        url: 'https://retire.wshoto.com/wx/imageupload',
        complete: (res) => {
          console.log(res)
          let resArr = JSON.parse(res.data).url
          this.setData({
            imgs: resArr
          })
          resolve(res)
        }
      })
    })
    promiseArr.push(promise)
    Promise.all(promiseArr).then(res => {
      let login_photo = this.data.imgs
      wx.request({
        url: 'https://retire.wshoto.com/wx/facelogin',
        data: {
          access_token: access_token,
          login_photo: login_photo
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if(res.data.code == 1){
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel:false,
              success: (res) => {
                console.log(res)
                if(res.confirm){
                  wx.switchTab({
                    url: '../index/index',
                  })
                } 
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel:false,
              success: (res) => {
                console.log(res)
              }
            })
          }
        }
      })
    })
  }
})